#############################################################################
# Copyright (c) 2009 Dmitry Grushin <dgrushin@gmail.com>.
#
# This file is part of clupower (cluster power manager utility)
#
# Clupower is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Clupower is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Clupower.  If not, see <http://www.gnu.org/licenses/>.
#
# Contributors:
#    Dmitry Grushin <dgrushin@gmail.com> - initial API and implementation
#############################################################################

$:.unshift File.join(File.dirname(__FILE__),'..','lib')

require 'commands'

class TestCommands < Test::Unit::TestCase

  def setup
    $settings_path = File.dirname(__FILE__)
    $settings_file = "settings.test1.yaml"

    Settings.instance.load
  end

  def test_cmd_get_reserved_nodes
    rnodes = Cmd.instance.get_reserved_nodes
    assert(rnodes.size == 2)
    assert(rnodes["node1"] == "SYSTEM.2")
    assert(rnodes["node2"] == "SYSTEM.0")
  end

  def test_cmd_get_free_nodes
    nodes = Cmd.instance.get_free_nodes
    assert(nodes.size == 1)
    assert(nodes.include?("node3.ispras.ru"))
  end

  def test_cmd_get_queue
    jobs = Cmd.instance.get_queue
    assert(jobs.size == 2)
    assert(jobs[0] == ["39", "H"])
    assert(jobs[1] == ["43", "Q"])
  end

  def test_cmd_get_queued_jobs
    jobs = Cmd.instance.get_queued_jobs
    assert(jobs.size == 1)
    assert(jobs[0] == "43")
  end

  def test_cmd_get_cluster_ppn
    assert(Cmd.instance.get_cluster_ppn == 1)
  end

  def test_cmd_get_job_details
    job = Cmd.instance.get_job_details("any_job")
    assert(job.interactive)
    assert(job.submit_time == 1248234371)
    assert(job.walltime == 600)
    assert(job.nodes == 1)
    assert(job.state == "R")
  end

  def test_cmd_ipmi_node_name
    assert(Cmd.instance.ipmi_node_name("n1") == "n1-ipmi")
    assert(Cmd.instance.ipmi_node_name("n1.xxx") == "n1-ipmi.xxx")
    assert(Cmd.instance.ipmi_node_name("n1.xxx.yyy") == "n1-ipmi.xxx.yyy")
  end

  def test_cmd_short_node_name
    assert(Cmd.instance.short_node_name("n1.ispras.ru") == "n1")
    assert(Cmd.instance.short_node_name("n1") == "n1")
  end

  def test_cmd_chassis_powered
    assert(Cmd.instance.chassis_powered?("n1.ispras.ru"))
  end

  def test_cmd_get_node_mac
    Settings.instance.config['WAKEUP'] = "wol"
    Settings.instance.config['CMD_SSH'] = "test/dumps/ssh"
    Settings.instance.config['CMD_IFCONFIG'] = "test/dumps/ifconfig.out"
    assert(Cmd.instance.get_node_mac_address("node") == "00:11:2F:DA:BF:97", "Incorrect MAC address")
    Cmd.instance.node_on("localhost")
    assert_raise CPE8  do
      Cmd.instance.node_on("afdsnflj")
    end
  end

  def test_cmd_ignored_nodes_online
    assert(Cmd.instance.ignored_nodes_online == 2)

    Settings.instance.config['IGNORE_NODES'] = ""
    assert(Cmd.instance.ignored_nodes_online == 0)

    Settings.instance.config['CMD_PBSNODES'] = "test/dumps/pbsnodes2"
    Settings.instance.config['IGNORE_NODES'] = "node3"
    assert(Cmd.instance.ignored_nodes_online == 0)
  end

  def test_errors
    assert_raise CPE11  do
      Settings.instance.config['CMD_PBSNODES'] = "unknown"
      Cmd.instance.node_off("node")
    end

    assert_raise CPE8  do
      Settings.instance.config['CMD_IPMI_WAKEUP'] = "unknown"
      Cmd.instance.node_on("node")
    end

    assert_raise CPE9 do
      Settings.instance.config['CMD_PBSNODES'] = "unknown"
      Cmd.instance.torque_node_online("node")
    end

    assert_raise CPE6 do
      Settings.instance.config['CMD_PBSNODES'] = "unknown"
      Cmd.instance.get_sleep_nodes
    end

    assert_raise CPE5 do
      Settings.instance.config['CMD_PBSNODES'] = "echo \\<ashdkj\\>\\</\\>"
      Cmd.instance.get_sleep_nodes
    end
  end

  def test_cmd_get_sleep_nodes
    Settings.instance.config["CMD_PBSNODES"] = "test/dumps/pbsnodes_multi"
    Settings.instance.config["IGNORE_NODES"] = ""
    nodes = Cmd.instance.get_sleep_nodes
    assert(nodes.size == 1)
    assert(nodes.include?("node1.ispras.ru"))
  end

  def test_is_gm_mapper_started
    Settings.instance.config['CMD_SSH'] = "test/dumps/ssh_exec"
    assert(Cmd.instance.is_gm_mapper_started(["n1.ispras.ru", "n2.ispras.ru", "n3.ispras.ru"], []))
  end

  def test_get_uptime
    Settings.instance.config['CMD_SSH'] = "test/dumps/ssh_exec"
    assert(Cmd.instance.get_uptime('node1') == 1000)
  end

  def test_deferred
    Settings.instance.config['CMD_CHECK_JOB'] = "test/dumps/checkjob2"
    assert(Cmd.instance.job_deferred?('job'))
  end

 def test_idle_time
   assert(Cmd.instance.get_node_idle_time('node1') == 0)
   Cmd.instance.update_nodes_idle_timers
   sleep(3)
   Cmd.instance.update_nodes_idle_timers
   assert(Cmd.instance.get_node_idle_time('node3.ispras.ru') >= 3)
   Settings.instance.config['CMD_PBSNODES'] = "test/dumps/pbsnodes2"
   Cmd.instance.update_nodes_idle_timers
   assert(Cmd.instance.get_node_idle_time('node3.ispras.ru') == 0)
 end
end
